#include  "event.h"
#include  "kernev.h"
#include  "doxon.h"

Event::Event(IVTNo _int_num, InterruptHandler int_handler){
    lock();
    myImpl = new KernelEv;

    myImpl->owner   = (PCB*)Doxon::running;
    myImpl->int_num = _int_num;
    myImpl->old_routine = (InterruptRoutine)Doxon::replace_int(_int_num
                                         , (InterruptRoutine)int_handler);
    unlock();
}

Event::~Event() {
    lock();
    Doxon::replace_int(myImpl->int_num, myImpl->old_routine);
    delete myImpl;
    unlock();
}

int Event::wait(Time time) {
    int return_val;
    lock();
    /* Samo vlasnik dogadjaja moze na njemu da se blokira */
    if ( Doxon::running == myImpl->owner ) {
        return_val = myImpl->event_sem->wait(time);
    }
    else {
        return_val = 0;
    }
    unlock();
    return return_val;
}

void Event::signal() {
    lock();
    myImpl->event_sem->signal();
    oldHandler();
    unlock();
}
void Event::oldHandler() {
    myImpl->old_routine();
}
